public with sharing class MassUpdateAddressController {

    public List<SelectOption> accountAddressTypeList{get;set;}
    public List<SelectOption> contactAddressTypeList{get;set;}

    public String accountAddressType{get;set;}
    public String contactAddressType{get;set;}


    public Account account{get;set;}

    public class ContactCheck{
        public boolean isUpdate{get;set;}
        public Contact contact{get;set;}
    }

    public List<ContactCheck> contactCheckList{get;set;}

    public ApexPages.StandardController controller{get;set;}
    
    public MassUpdateAddressController(ApexPages.StandardController controller) {
        this.controller = controller;
        
        accountAddressTypeList = new List<SelectOption>();
        contactAddressTypeList = new List<SelectOption>();
        accountAddressTypeList.add(new SelectOption('billing','請求先'));
        accountAddressType = 'billing';
        accountAddressTypeList.add(new SelectOption('shipping','納入先'));
        contactAddressTypeList.add(new SelectOption('mailing','郵送先'));
        contactAddressType = 'mailing';
        contactAddressTypeList.add(new SelectOption('other','その他'));
        
        
        account = [SELECT id,name,billingcountry,billingstate,billingcity,billingstreet,billingpostalcode
                            ,shippingcountry,shippingstate,shippingcity,shippingstreet,shippingpostalcode
                            ,(SELECT id,name,title,department,email
                                ,mailingcountry,mailingstate,mailingcity,mailingstreet,mailingpostalcode 
                                ,othercountry,otherstate,othercity,otherstreet,otherpostalcode                                 
                                FROM contacts LIMIT 2000) FROM account WHERE id = :controller.getId()];

        contactCheckList = new List<contactCheck>();
        for(contact con : account.contacts){
            ContactCheck contactcheck = new ContactCheck();            
            contactcheck.contact = con;
            contactCheck.isupdate = false;
            contactCheckList.add(contactcheck);
        }
        
        if(contactCheckList.size() >= 2000){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'この取引先には取引先責任者が2000人以上います。全てのデータを表示する事はできません'));
        }
    }
    
    
    public PageReference massupdate(){
        List<contact> updates = new List<contact>();
        for(ContactCheck contactCheck : contactCheckList){
            if(contactCheck.isupdate){
                Contact contact = updateContactField(contactCheck.contact,accountAddressType,contactAddressType);
                updates.add(contact);
            }            
            if(updates.size() >=200){
                update updates;
                updates  = new List<contact>();
            }
        }
        update updates;
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, '取引先責任者の住所更新が完了しました'));
        return null;
    }
    
    
    private Contact updateContactField(Contact contact,String accountAddressType,String contactAddressType){    
        String fromCountry;
        String fromPostalCode;
        String fromState;
        String fromCity;
        String fromStreet;

        if(accountAddressType == 'billing'){
            fromCountry = account.billingCountry;
            fromPostalCode = account.billingPostalCode;
            fromState = account.billingState;
            fromCity = account.billingCity;
            fromStreet = account.billingStreet;
        }else{
            fromCountry = account.shippingCountry;
            fromPostalCode = account.shippingPostalCode;
            fromState = account.shippingState;
            fromCity = account.shippingCity;
            fromStreet = account.shippingStreet;
        }

        if(contactAddressType == 'mailing'){
            contact.mailingCountry = fromCountry;
            contact.mailingPostalCode = fromPostalCode;
            contact.mailingState = fromState;
            contact.mailingCity = fromCity;
            contact.mailingStreet = fromStreet;
        }else{
            contact.otherCountry = fromCountry;
            contact.otherPostalCode = fromPostalCode;
            contact.otherState = fromState;
            contact.otherCity = fromCity;
            contact.otherStreet = fromStreet;
        }
    
        return contact;
    }
    
}